package cn.piesat.controller;

import cn.piesat.dto.PiesatUserInfoDTO;
import cn.piesat.service.intf.UserInfoService;
import cn.piesat.util.ResponseWrapperUtil;
import cn.piesat.vo.userInfoConfig.UserListVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.piesat.vo.userInfoConfig.UpdatePasswordParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 用户管理接口
 *
 * @author xhl
 */
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 添加用户
     * {
     *     "deptId":"",
     *     "roleIdList":[],
     *     "lockState":"",
     *     "配置中的key":"value"
     * }
     * @return
     */
    @PostMapping("/ordinary/userinfo/add/user")
    public String add(@RequestBody JSONObject params) {
        Map<String,String> resultMap = null;
        try {
            resultMap = userInfoService.addUser(params);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","添加用户接口异常：" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 修改用户信息
     * {
     *     "deptId":"",
     *     "roleIdList":[],
     *     "lockState":"",
     *     "配置中的key":"value"
     * }
     * @return
     */
    @PostMapping("/manager/userinfo/update/user")
    public String update(@RequestBody JSONObject params) {
        Map<String,String> resultMap = null;
        try {
            resultMap = userInfoService.updateUser(params);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","修改用户接口异常：" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    @GetMapping("/manager/userinfo/del/{id}")
    public String deleteOne(@PathVariable("id") String id) {
        try {
            userInfoService.deleteOne(id);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","删除用户接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeResultTips(true);
    }

    /**
     * {
     *     "roleIdList":[],
     *     "deptId":"",
     *     "配置的搜索项":""
     * }
     * @param pageable
     * @param params
     * @return
     */
    @PostMapping("/manager/userinfo/search")
    public String search(Pageable pageable, @RequestBody JSONObject params) {
        Map<String,Object> pageMap = userInfoService.search(params,pageable);
        return ResponseWrapperUtil.executeResultMap(pageMap);
    }

    /**
     * {
     *     "id":"",//用户ID
     *     "isLock":"true/false"//锁定/解锁
     * }
     * @param params
     * @return
     */
    @PostMapping("/manager/userinfo/lock")
    public String lockUser(@RequestBody JSONObject params) {
        try {
            userInfoService.lock(params);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","锁定用户接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeResultTips(true);
    }

    /**
     * {
     *     "userId":"用户ID"
     * }
     * @param params
     * @return
     */
    @PostMapping("/manager/userinfo/reset")
    public String resetPassword(@RequestBody JSONObject params) {
        String restPass = null;
        try {
            restPass = userInfoService.resetPassword(params.getString("userId"));
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","重置密码接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeResultEntity(restPass);
    }

    /**
     * 获取用户详细信息
     * @param userId
     * @return
     */
    @GetMapping("/manager/userinfo/get/{userId}")
    public String findByUserId(@PathVariable("userId") String userId) {
        Map<String,Object> resultMap = userInfoService.findByUserId(userId);
        return ResponseWrapperUtil.executeResultMap(resultMap);
    }

    /**
     * 获取用户所拥有的菜单
     * @param request
     * @return
     */
    @GetMapping("/permission/getPermission")
    public String getUserMenu(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if (StringUtils.isBlank(userId)) {
            return ResponseWrapperUtil.executeCustomTips("false","无效用户");
        }
        JSONArray menuTree = userInfoService.getPermissionMenu(userId);
        return ResponseWrapperUtil.executeResultList(menuTree);
    }

    /**
     * 获取登录后用户基本信息
     * @param request
     * @return
     */
    @GetMapping("/manager/userinfo/getUser")
    public String getLoginUserInfo(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        if (StringUtils.isBlank(userId)) {
            return ResponseWrapperUtil.executeCustomTips("false","无效用户");
        }
        Map<String,Object> resultMap = userInfoService.findLoginUserInfo(userId);
        return ResponseWrapperUtil.executeResultMap(resultMap);
    }

    /**
     * 获取当前用户下的菜单中权限按钮标识
     */
    @GetMapping("permission/getButtonMark/{menuId}")
    public String getMenuButtonMark(HttpServletRequest request,@PathVariable("menuId") String menuId) {
        String userId = request.getHeader("userId");
        if (StringUtils.isBlank(userId)) {
            return ResponseWrapperUtil.executeCustomTips("false","无效用户");
        }
        List<String> resultList = userInfoService.getMenuButtonList(userId,menuId);
        return ResponseWrapperUtil.executeResultList(resultList);
    }

    /**
     * 修改登录用户密码
     * @param request
     * @param passwordParam
     * @return
     */
    @PostMapping("/ordinary/userinfo/update")
    public String updatePassword(HttpServletRequest request,@RequestBody UpdatePasswordParam passwordParam) {
        String userId = request.getHeader("userId");
        if (StringUtils.isBlank(userId)) {
            return ResponseWrapperUtil.executeCustomTips("false","无效用户");
        }
        Map<String,String> resultMap = null;
        try {
            resultMap = userInfoService.updatePassword(passwordParam,userId);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","修改密码接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 查询所有未锁定的用户
     * @param request
     * @return
     */
    @GetMapping("/ordinary/userinfo/findUserList")
    public String findUserList(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        List<PiesatUserInfoDTO> userList = userInfoService.findUserList();
        List<UserListVo> result = userList.stream().map(dto -> {
            UserListVo userListVo = new UserListVo();
            userListVo.setId(dto.getId());
            userListVo.setUserName(dto.getUsername());
            userListVo.setCurrentUser(dto.getId().equals(userId));
            return userListVo;
        }).collect(Collectors.toList());
        return ResponseWrapperUtil.executeResultList(result);
    }

    /**
     * 查询今日登录人数 总用户数 最近登录用户
     */
    @GetMapping("/manager/userinfo/charts")
    public String getCharts(){
        Map<String, Object> charts = userInfoService.getCharts();
        return ResponseWrapperUtil.executeResultMap(charts);
    }

    /**
     * @Description: 查询值班人员，提供给1043服务
     * @param: []
     * @return: java.lang.String
     **/
    @GetMapping("/manager/userinfo/allNameList")
    public String allNameList(){
        List<String> names = userInfoService.listAllUserName();
        return ResponseWrapperUtil.executeResultList(names);
    }
}
